package com.cooba.content_frament;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.video_frament.FilmFrament;
import com.cooba.video_frament.RecommendFrament;
import com.cooba.video_frament.SmileFrament;
import com.cooba.video_frament.VarietyFrament;
import com.cooba.video_frament.VideoHotFrament;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/7.
 * 视频
 */
public class VideoFrament extends Fragment {

    private HorizontalScrollView scrollView;
    private ViewPager pager;
    private TextView indicator;
    private RadioGroup rg;
    private List<Fragment> fragList = new ArrayList<Fragment>();
    private MyFragmentAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    private View contentview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (contentview == null) {
            contentview = initview(inflater);
        }
        return contentview;
    }

    //初始化控件
    private View initview(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.video_frament, null, false);
        initFragment();
        scrollView = (HorizontalScrollView) view.findViewById(R.id.scroll_video);
        pager = (ViewPager) view.findViewById(R.id.pager_video);
        indicator = (TextView) view.findViewById(R.id.indicator_video);
        rg = (RadioGroup) view.findViewById(R.id.rg_video);
        adapter = new MyFragmentAdapter(getActivity().getSupportFragmentManager());
        pager.setAdapter(adapter);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                // 选中的RadioButton播放动画
                ScaleAnimation sAnim = new ScaleAnimation(1, 1.1f, 1, 1.1f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                sAnim.setDuration(2000);
                sAnim.setFillAfter(true);

                // 遍历所有的RadioButton
                for (int i = 0; i < group.getChildCount(); i++) {
                    RadioButton radioBtn = (RadioButton) group.getChildAt(i);
                    if (radioBtn.isChecked()) {
                        radioBtn.startAnimation(sAnim);
                    } else {
                        radioBtn.clearAnimation();
                    }
                }

                switch (checkedId) {
                    case R.id.rg_Recommend:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.rg_Hot:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.rg_Film:
                        pager.setCurrentItem(2);
                        break;
                    case R.id.rg_Smile:
                        pager.setCurrentItem(3);
                        break;
                    case R.id.rg_Variety:
                        pager.setCurrentItem(4);
                        break;
                    default:
                        break;
                }
            }
        });

        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // 获取对应位置的RadioButton
                RadioButton radioBtn = (RadioButton) rg.getChildAt(arg0);
                // 设置对应位置的RadioButton为选中的状态
                radioBtn.setChecked(true);
                /* 滚动HorizontalScrollView使选中的RadioButton处于屏幕中间位置 */
                // 获取屏幕信息
                DisplayMetrics outMetrics = new DisplayMetrics();
                getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
                // 获取每一个RadioButton的宽度
                int radioBtnPiexls = radioBtn.getWidth();
                // 计算滚动距离
                int distance = (int) ((arg0 + 0.5) * radioBtnPiexls - outMetrics.widthPixels / 2);
                // 滚动
                scrollView.scrollTo(distance, 0);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) indicator.getLayoutParams();

                params.leftMargin = (int) ((arg0 + arg1) * params.width);
                indicator.setLayoutParams(params);

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });


        return view;
    }


    class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragList.get(arg0);
        }

        @Override
        public int getCount() {
            return fragList.size();
        }

    }

    /**
     * 初始化 Fragment
     */
    public void initFragment() {
        fragList = new ArrayList<Fragment>();
        RecommendFrament fragment1 = new RecommendFrament();
        VideoHotFrament fragment2 = new VideoHotFrament();
        FilmFrament fragment3 = new FilmFrament();
        SmileFrament fragment4 = new SmileFrament();
        VarietyFrament fragment5 = new VarietyFrament();
        fragList.add(fragment1);
        fragList.add(fragment2);
        fragList.add(fragment3);
        fragList.add(fragment4);
        fragList.add(fragment5);
    }

}
